{$CLEO .cs}

thread 'BEAR'

:BEAR_11
wait 0
if 
  Player.Defined($PLAYER_CHAR)
then
  if 
    $ONMISSION == 0
  then
    load_special_character 'NEXTBOT' as 5
    load_special_character 'NEXTBOT' as 6
    request_animation "BEAR_ANIMATION"
    wait 0
    if and
      has_special_character_loaded 5
      has_animation_loaded "BEAR_ANIMATION"
    then
      jump @BEAR_Keys
    end
  end
end
jump @BEAR_11

:BEAR_Keys
wait 0
if
  0AB0: key_pressed 78 // "N" key
then
  get_offset_from_char_in_world_coords $X $Y $Z from_actor $PLAYER_ACTOR with_offset 0.0 -7.0 0.0
  jump @BEAR_Spawn
end
jump @BEAR_11

:BEAR_Spawn
Actor.Create(2@, PedType.Mission1, 295, $X, $Y, $Z)
Actor.Angle(2@) = 254.662
Actor.Health(2@) = 3000
set_char_suffers_critical_hits 2@ dismemberment_possible 0
set_char_drowns_in_water 2@ drowns_in_water 0
set_char_uses_upperbody_damage_anims_only 2@ actions_uninterupted_by_weapon_fire 1
set_char_decision_maker 2@ decision_maker_to 32
set_char_money 2@ money 0
task_hand_gesture 2@ rotate_to_and_look_at_actor $PLAYER_ACTOR
wait 100
open_sequence_task 15@
perform_sequence_task 2@ to_AS_pack 15@
task_play_anim -1 perform_animation "BEAR_STAND" IFP "BEAR_ANIMATION" framedelta 4.0 loop 1 lockX 0 lockY 0 lockF 0 time -1
close_sequence_task 15@
clear_sequence_task 15@
wait 200
$BEAR_ACTOR = Actor.Health(2@)
$BEAR_ACTOR /= 40
task_play_anim_non_interruptable 2@ perform_animation "SAWNOFF_RELOAD" IFP "BEAR_ANIMATION" framedelta 4.0 loopA 1 lockX 1 lockY 1 lockF 0 time -1
unload_special_character 5
8@ = 0
jump @BEAR_Main_Loop

:BEAR_Main_Loop
wait 0
if 
  Player.Defined($PLAYER_CHAR)
then
  if 
    not Actor.Dead(2@)
  then
    // Detectar si el jugador est cerca
    if 
      locate_char_any_means_char_2d $PLAYER_ACTOR near_actor 2@ radius 200.0 200.0 0
    then
      // Realizar acciones cuando el jugador est cerca del oso
      if and
        8@ == 0
        locate_char_any_means_char_2d $PLAYER_ACTOR near_actor 2@ radius 2.0 2.0 0
      then
        // Iniciar ataque cuando el jugador est muy cerca
        task_play_anim_non_interruptable 2@ perform_animation "BEAR_ATTACK" IFP "BEAR_ANIMATION" framedelta 4.0 loopA 1 lockX 1 lockY 1 lockF 0 time -1
        8@ = 1
      end
      if and
        8@ == 1
        not locate_char_any_means_char_2d $PLAYER_ACTOR near_actor 2@ radius 2.0 2.0 0
      then
        // Dejar de atacar cuando el jugador se aleja
        task_play_anim_non_interruptable 2@ perform_animation "SAWNOFF_RELOAD" IFP "BEAR_ANIMATION" framedelta 4.0 loopA 1 lockX 1 lockY 1 lockF 0 time -1
        8@ = 0
      end
      
      // Ajustar velocidad de animaciones segn las circunstancias
      3@ = 1.0
      if 
        locate_char_any_means_char_2d $PLAYER_ACTOR near_actor 2@ radius 8.0 8.0 0
      then
        3@ = 0.7
      end
      if 
        Actor.Animation(2@) == "SAWNOFF_RELOAD"
      then
        set_char_anim_speed 2@ perform_animation "SAWNOFF_RELOAD" at 3@
      end
      if 
        Actor.Animation(2@) == "BEAR_ATTACK"
      then
        set_char_anim_speed 2@ perform_animation "BEAR_ATTACK" at 0.5
        get_char_anim_current_time 31@ = actor 2@ animation "BEAR_ATTACK"
        if and
          31@ > 0.4
          0.5 > 31@
          locate_char_any_means_char_2d $PLAYER_ACTOR near_actor 2@ radius 2.5 2.5 0
        then
          get_char_armour 7@ = actor $PLAYER_ACTOR
          if 
            7@ > 0
          then
            Actor.Armour($PLAYER_ACTOR) = -5
            task_say $PLAYER_ACTOR speak_from_audio_table 344
          else
            damage_char $PLAYER_ACTOR decrease_health_by 5 affect_armour 1
            task_say $PLAYER_ACTOR speak_from_audio_table 344
          end
        end
      end
      
      // Ajustar la orientacin del oso hacia el jugador
      Actor.StorePos(2@, 11@, 12@, 13@)
      Actor.StorePos($PLAYER_ACTOR, 21@, 22@, 23@)
      sub_float_lvar_from_float_lvar 11@ -= 21@
      sub_float_lvar_from_float_lvar 12@ -= 22@
      get_heading_from_vector_2d 11@ 12@ store_to 30@
      30@ += 180.0
      Actor.Angle(2@) = 30@
      
      // Verificar si el oso ha recibido suficiente dao para morir
      $BEAR_ACTOR = Actor.Health(2@)
      $BEAR_ACTOR /= 25
      if 
        0 >= $BEAR_ACTOR
      then
        task_die_named_anim 2@ perform_animation "BEAR_STAND" IFP_file "BEAR_ANIMATION" 4.0 time -2
        jump @BEAR_Cleanup
      end
      
      // Verificar si el jugador ha muerto
      if 
        Actor.Dead($PLAYER_ACTOR)
      then
        // Limpiar y destruir el oso si el jugador est muerto
        jump @BEAR_Cleanup
      end
      
      // Continuar en el bucle principal
      jump @BEAR_Main_Loop
    end
  end
end

// Volver a la etiqueta inicial para reiniciar el bucle
jump @BEAR_11

:BEAR_Cleanup
Actor.DestroyWithFade(2@)
remove_decision_maker 32
8@ = 0 // Reiniciar variable de estado si es necesario
jump @BEAR_11
